#ifndef _SREEN_
#define _SREEN_

#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include "img.h"

Adafruit_SSD1306 oled(128, 64, &Wire);

class Sreen
{
  int lastTime;
public:
void init()
{
  oled.begin(SSD1306_SWITCHCAPVCC, 0x3C); 
  oled.clearDisplay();
  showSysInit(1);
}
//系统初始化过程
void showSysInit(int secTime)
{
  oled.drawBitmap(0, 0, img_init2, 128, 64, BLACK, WHITE);
  oled.display();
  delay(500);
  oled.startscrollright(0x00, 0x0F);
  delay(500);
  oled.startscrollleft(0x00, 0x0F);
  delay(500);
  oled.startscrollleft(0x00, 0x0F);
  delay(500);
  oled.startscrollright(0x00, 0x0F);
  delay(500);
  oled.stopscroll();
  
  secTime *= 1000;    //转ms
  secTime /= 100;     //刻度延时
  for(int i = 0;i <= 50;i ++)
  {
    oled.clearDisplay();
    oled.drawBitmap(0, 0, img_init, 128, 64, BLACK, WHITE);
    oled.drawLine(0, 50, 127, 50, SSD1306_WHITE);
    oled.drawLine(0, 63, 127, 63, SSD1306_WHITE);
    oled.setTextColor(SSD1306_WHITE);
    oled.setCursor(25,53);
    oled.setTextSize(1);
    //oled.drawBitmap(0,0,chineseCLib,16,64,WHITE); 
    oled.print("Sys Init... ");
    oled.print(i*2);
    oled.print("%");
    oled.fillRect(0, 51, map(i*2,0,100,0,127), 11, SSD1306_INVERSE);
    oled.display();
    delay(secTime*2);
  }
}
//显示wifi连接中
void showWifiConnect(const char *ssid,const char *pwd)
{
  oled.clearDisplay();
  oled.setTextColor(SSD1306_WHITE);
  oled.setCursor(5, 0);
  oled.setTextSize(1); 
  oled.print("Connect to Wi-Fi...");
  
  oled.setCursor(5, 20);
  oled.setTextSize(1);
  oled.print("SSID:");
  oled.print(ssid);
  
  oled.setCursor(5, 40);
  oled.setTextSize(2);
  oled.print("PWD:******");
  oled.display();
}
//右上角或左上角显示当前状态保持的时间
void showImgTime(int x,int y,unsigned int timeSec)
{
  oled.setCursor(x, y);
  oled.setTextSize(2);
  if(timeSec < 300)
  {
      oled.print(timeSec);
      oled.setCursor(oled.getCursorX(), 8);
      oled.setTextSize(1); 
      oled.print("s");
      return ;
  }
  timeSec /= 60; 
  oled.print(timeSec);
  oled.setCursor(oled.getCursorX(), 8);
  oled.setTextSize(1); 
  oled.print("min");
}
//显示温湿度函数
void showTimeDHT11(float tem,float hum,const RtcDateTime *nowTime,bool isWarning)
{
  String date = "",time = "";
  date += nowTime->Year(); 
  date += "-";  date += nowTime->Month(); 
  date += "-";  date += nowTime->Day();
  
  time += nowTime->Hour(); 
  time += ":";  time += nowTime->Minute(); 
  time += ":";  time += nowTime->Second();

  oled.clearDisplay();
  oled.setTextColor(SSD1306_WHITE);
  
  oled.drawBitmap(0,0,time_log,32,32,WHITE);  //时间符号
  oled.setCursor(50, 2); oled.setTextSize(1);
  oled.print(date);

  oled.setCursor(32, 13); oled.setTextSize(2); 
  oled.print(time);
  
  oled.drawBitmap(0,32,tem_log,24,32,WHITE);  //温度符号
  oled.setCursor(25, 48); oled.setTextSize(1); 
  String temStr = (tem == -999) ? String("--"):String(tem,1);
  oled.print(temStr);
  oled.drawBitmap(50,46,temLib,12,12,WHITE);  //摄氏度符号
  
  oled.drawBitmap(65,32,hum_log,24,32,WHITE); //湿度符号
  oled.setCursor(93, 48); oled.setTextSize(1); 
  String humStr = (hum == -999) ? String("--"):String(hum,0);
  oled.print(humStr);
  oled.print(" %");
  
  oled.drawLine(0, 31, 127, 31, SSD1306_WHITE);
  oled.drawLine(63, 31, 63, 63, SSD1306_WHITE);

  if(isWarning && nowTime->Second()%2)
    oled.drawRect(0, 0, 128, 64, SSD1306_WHITE);
  oled.display();
}
void showImg(const unsigned char *data)
{
  oled.drawBitmap(0, 0, data, 128, 64, BLACK, WHITE);
  oled.display();
}

void showTextTime(int timeSec,String str)
{
  oled.clearDisplay();
  oled.setTextColor(SSD1306_WHITE);
  
  showImgTime(0,0,timeSec);
  
  oled.setCursor(5, 32);
  oled.print(str);

  if(timeSec%2)
    oled.fillRect(0, 0, 128, 64, SSD1306_INVERSE);
    
  oled.display();
}

void updateDrinking(int timeSec) // 喝水中 (持续时间最大无限制)
{    
    if(timeSec == lastTime)
      return ;
    lastTime = timeSec;
    oled.clearDisplay();

    int posX = 0;
    if(timeSec < 30)    //正常喝水状态
      oled.drawBitmap(0, 0, img_drinking[0], 128, 64, BLACK, WHITE);
    else if(timeSec < 60) //提醒放回原位
    {
      oled.drawBitmap(0, 0, img_drinking[1], 128, 64, BLACK, WHITE);
      posX = 86;
    }
    else if(timeSec < 120) //接水中？
      oled.drawBitmap(0, 0, img_drinking[2], 128, 64, BLACK, WHITE);
    else if(timeSec < 300) //杯子碎了
    {
      oled.drawBitmap(0, 0, img_drinking[3], 128, 64, BLACK, WHITE);
      posX = 86;
    }
    else //长时间未使用，建议关机
      oled.drawBitmap(0, 0, img_drinking[4], 128, 64, BLACK, WHITE);
    
    //绘制时间
    oled.setTextColor(SSD1306_WHITE);
    showImgTime(posX,0,timeSec);
    oled.display();
}

void updateWaitting(int timeSec)
{
    if(timeSec == lastTime)
      return ;
    lastTime = timeSec;
    oled.clearDisplay();

    if(timeSec <= 6)    //显示杯子放入的状态
    {
        oled.drawBitmap(0, 0, img_waiting[0], 128, 64, BLACK, WHITE);
        showImgTime(0,0,timeSec);
        oled.display();
    }
    else if(timeSec < 30)   
    {
        oled.drawBitmap(0, 0, img_waiting[1], 128, 64, BLACK, WHITE);
//        showImgTime(96,0,timeSec);
        oled.display();
    }
    else   
    {
      if(timeSec % 30 == 0)
      {
        int imgIndex = random(0,12);
        oled.drawBitmap(0, 0, img_words[imgIndex], 128, 64, BLACK, WHITE);
        oled.display();
      }
      return ;
    }
}
void updateOutTime_1(int timeSec)    // 喝水超时，最大60min
{    
    if(timeSec == lastTime)
      return ;
    lastTime = timeSec;
    oled.clearDisplay();
    
    //随机显示对应的图片
    static int index = 0;
    if(timeSec % 30 == 0)
      index = random(0,2);
    showImg(img_outTime_1[index]);
     
    //屏幕闪烁
    if(timeSec%2)
    oled.fillRect(0, 0, 128, 64, SSD1306_INVERSE);
    oled.display(); 
}
void updateOutTime_2(int timeSec)
{
    if(timeSec == lastTime)
      return ;
    lastTime = timeSec;
    oled.clearDisplay();

    //随机显示对应的图片
    static int index = 0;
    if(timeSec % 60 == 0)
      index = random(0,2);
    showImg(img_outTime_2[index]);

    //屏幕闪烁
    if(timeSec%2)
    oled.fillRect(0, 0, 128, 64, SSD1306_INVERSE);
    oled.display();   
}
void updateOutTime_3(int timeSec) // 喝水超时，最大60min
{
    if(timeSec == lastTime)
      return ;
    lastTime = timeSec;
    oled.clearDisplay();

    showTextTime(timeSec,"Please drink some water...");
}

void updateOutTime_4(int timeSec) // 喝水超时，最大60min 
{    
    if(timeSec == lastTime)
      return ;
    lastTime = timeSec;
    oled.clearDisplay();

    //建议关机
    oled.drawBitmap(0, 0, img_drinking[4], 128, 64, BLACK, WHITE);
    oled.display();
}

void updateOutTime_5(int timeSec) // 喝水超时，持续时间最大无限制
{ 
    if(timeSec == lastTime)
      return ;
    lastTime = timeSec;
    oled.clearDisplay();
    //直接关机处理，无限制深入休眠
}

};

Sreen sreen;            //屏幕显示对象
#endif
